package com.kobe.game_60;

import java.util.ArrayList;
import java.util.List;

import com.kobe.util.Prime;

/**
 * 
 * The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 +
 * 5 + 7 + 11 + 13
 * 
 * This is the longest sum of consecutive primes that adds to a prime below
 * one-hundred.
 * 
 * The longest sum of consecutive primes below one-thousand that adds to a
 * prime, contains 21 terms, and is equal to 953.
 * 
 * Which prime, below one-million, can be written as the sum of the most
 * consecutive primes?
 * 
 * 
 */
public class _50 {

    public static void main(String[] args) {

        List<Integer> primes = new ArrayList<Integer>();

        for (int i = 1; i < 1000000; i++) {
            if (Prime.isPrime(i)) {
                primes.add(i);
            }
        }

        int length = 0;
        int result = 0;
        Loop: for (int i = 0; i < primes.size(); i++) {
            int sum = 0;
            for (int m = i; m < primes.size(); m++) {
                sum += primes.get(m);
                if (sum >= 1000000) {
                    continue Loop;
                }
                if (Prime.isPrime(sum)) {
                    if (m - i > length) {
                        length = m;
                        result = sum;
                    }
                }
            }
        }

        System.out.println(result);
    }
}
